package common

import (
	"gorm.io/gorm"
	"likeadmin/config"
	"likeadmin/core"
	"likeadmin/core/response"
	"likeadmin/model/client"
	"likeadmin/util"
	"time"
)

type IIndexService interface {
	Console() (res map[string]interface{}, e error)
	Config() (res map[string]interface{}, e error)
}

// NewIndexService 初始化
func NewIndexService(db *gorm.DB) IIndexService {
	return &indexService{db: db}
}

// indexService 主页服务实现类
type indexService struct {
	db *gorm.DB
}

// Console 控制台数据
func (iSrv indexService) Console() (res map[string]interface{}, e error) {
	// 版本信息
	name, err := util.ConfigUtil.GetVal(iSrv.db, "website", "name", "LikeAdmin-Go")
	if e = response.CheckErr(err, "Console Get err"); e != nil {
		return
	}
	version := map[string]interface{}{
		"name":    name,
		"version": config.Config.Version,
		"website": "drama-admin.eeopn.com",
		"based":   "GO、Vue3.x、ElementUI、MySQL",
		"channel": map[string]string{
			"gitee":   "https://drama-admin.eeopn.com/",
			"website": "https://drama-admin.eeopn.com/",
		},
	}
	// 访客图表
	now := time.Now()

	// 获取今天开始时间
	todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())

	// 访问量(人)
	var todayUsers int64
	iSrv.db.Model(client.ClientUser{}).Where("create_time >= ?", todayStart).Count(&todayUsers)

	// 总访问量
	var totalUsers int64
	iSrv.db.Model(client.ClientUser{}).Count(&totalUsers)

	// 销售额(元)
	var todaySales uint
	iSrv.db.Model(client.Orders{}).Where("create_time >= ? AND is_pay = ?", todayStart, 1).Select("COALESCE(SUM(money), 0)").Scan(&todaySales)

	// 总销售额 - 获取订单金额总和
	var totalSales uint // 注意：金额通常使用浮点数
	iSrv.db.Model(client.Orders{}).Where("is_pay = ?", 1).Select("COALESCE(SUM(money), 0)").Scan(&totalSales)

	// 订单量(笔)
	var todayOrder int64
	iSrv.db.Model(client.Orders{}).Where("create_time >= ? AND is_pay = ?", todayStart, 1).Count(&todayOrder)

	// 总订单量
	var totalOrder int64
	iSrv.db.Model(client.Orders{}).Where("is_pay = ?", 1).Count(&totalOrder)

	// 今日数据
	today := map[string]interface{}{
		"time":       now.AddDate(0, 0, 0).Format(core.TimeFormat),
		"todaySales": core.OtMoney(todaySales), // 销售额(元)
		"totalSales": core.OtMoney(totalSales), // 总销售额
		"todayOrder": todayOrder,               // 订单量(笔)
		"totalOrder": totalOrder,               // 总订单量
		"todayUsers": todayUsers,               // 新增用户
		"totalUsers": totalUsers,               // 总访用户
	}

	var date []string
	for i := 14; i >= 0; i-- {
		date = append(date, now.AddDate(0, 0, -i).Format(core.DateFormat))
	}
	visitor := map[string]interface{}{
		"date": date,
		"list": []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	}
	return map[string]interface{}{
		"version": version,
		"today":   today,
		"visitor": visitor,
	}, nil
}

// Config 公共配置
func (iSrv indexService) Config() (res map[string]interface{}, e error) {
	website, err := util.ConfigUtil.Get(iSrv.db, "website")
	if e = response.CheckErr(err, "Config Get err"); e != nil {
		return
	}
	copyrightStr, err := util.ConfigUtil.GetVal(iSrv.db, "website", "copyright", "")
	if e = response.CheckErr(err, "Config GetVal err"); e != nil {
		return
	}
	var copyright []map[string]string
	if copyrightStr != "" {
		err = util.ToolsUtil.JsonToObj(copyrightStr, &copyright)
		if e = response.CheckErr(err, "Config JsonToObj err"); e != nil {
			return
		}
	} else {
		copyright = []map[string]string{}
	}
	return map[string]interface{}{
		"webName":     website["name"],
		"webLogo":     util.UrlUtil.ToAbsoluteUrl(website["logo"]),
		"webFavicon":  util.UrlUtil.ToAbsoluteUrl(website["favicon"]),
		"webBackdrop": util.UrlUtil.ToAbsoluteUrl(website["backdrop"]),
		"ossDomain":   config.Config.PublicUrl,
		"copyright":   copyright,
	}, nil
}
